package edu.scau.cxq.gp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.scau.cxq.gp.common.annotation.ResponseResult;
import edu.scau.cxq.gp.common.utils.WrapperUtils;
import edu.scau.cxq.gp.entity.dto.CustomerSignDTO;
import edu.scau.cxq.gp.entity.Customer;
import edu.scau.cxq.gp.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description 存储客户信息 前端控制器
 * @return
 * @author TLC
 */

@RestController
@ResponseResult
@RequestMapping("/customer")
public class CustomerController {

    @Resource
    private ICustomerService customerService;

    /**
     * 插入新客户
     * @param customer
     * @return
     */
    @PostMapping("/insertOne")
    public boolean insertOne(@RequestBody Customer customer) {
        return customerService.save(customer);
    }

    /**
     * 查找客户
     * @return
     */
    @GetMapping("/select")
    public List<Customer> select(Customer customer) {
        QueryWrapper queryWrapper = WrapperUtils.entitytoWrapper(customer);
        queryWrapper.eq("cus_hidden", 0);
        List<Customer> customers = customerService.list(queryWrapper);
        return customers;
    }

    /**
     * 查找单个客户
     */
    @GetMapping("/selectOne")
    public Customer selectOne (Long cusId) {
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cus_id", cusId);
        Customer customer = customerService.getOne(queryWrapper);

        return customer;
    }

    /**
     * 分页查找客户
     * @return
     */
    @GetMapping("/selectByPage")
    public IPage<Customer> selectByPage (Integer currentPage, Integer size, Customer customer) {
        Page<Customer> page = new Page<>(currentPage, size);
        QueryWrapper queryWrapper = WrapperUtils.entitytoWrapper(customer);
        queryWrapper.eq("cus_hidden", 0);
        return customerService.page(page, queryWrapper);
    }

    /**
     * 删除
     * @param customer
     * @return
     */
    @PostMapping("/deleteOne")
    public boolean deleteOne(@RequestBody Customer customer) {
        QueryWrapper queryWrapper = WrapperUtils.entitytoWrapper(customer);
        Customer cus = customerService.getOne(queryWrapper);
        // 将隐藏位置1
        cus.setCusHidden(1);
        return customerService.updateById(cus);
    }

    /**
     * 修改客户
     * @RequestBody：
     * 作用：
     * 主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的)；
     * @param customer
     * @return
     */
    @PostMapping("/updateOne")
    public boolean updateOne (@RequestBody Customer customer) {
        return customerService.updateById(customer);
    }

    /**
     * 获取所有的客户标签
     * @return
     */
    @GetMapping("/selectCustomerSigns")
    public List<CustomerSignDTO> selectCustomerSigns () {
        return customerService.findCustomerSigns();
    }
}
